package org.codetranslators.common;

import java.util.Vector;

public class Node implements Cloneable {
	private String name;

	private Vector<Node> predecessors;
	private Vector<Node> successors;
	private int positionInGraph;
	private Vector<Edge> inComingEdges;
	private Vector<Edge> outgoingEdges;

	public Node() {
		predecessors = new Vector<Node>();
		successors = new Vector<Node>();
		inComingEdges = new Vector<Edge>();
		outgoingEdges = new Vector<Edge>();
	}

	public Node(String name) {
		this.name = name;
	}

	public void addPredecessor(Node src) {
		predecessors.addElement(src);
	}

	public void addPredecessorAt(Node src, int indexToInsertPred) {
		predecessors.insertElementAt(src, indexToInsertPred);
	}

	public void addSuccessesor(Node dest) {
		successors.addElement(dest);
	}

	public void addIncomingEdge(Edge edge) {
		inComingEdges.addElement(edge);
	}

	public void addOutgoingEdge(Edge edge) {
		outgoingEdges.addElement(edge);
	}

	public Vector<Node> getSuccessors() {
		return successors;
	}

	public Vector<Node> getPredecessors() {
		return predecessors;
	}

	public int getNumOutgoingEdges() {
		return successors.size();
	}

	public int getNumIncomingEdges() {
		return predecessors.size();
	}

	public int getNumIncidentEdges() {
		return getNumOutgoingEdges() + getNumIncomingEdges();
	}

	public int getPositionInGraph() {
		return positionInGraph;
	}

	public void setPositionInGraph(int positionInGraph) {
		this.positionInGraph = positionInGraph;
	}

	public Vector<Edge> getInComingEdges() {
		return inComingEdges;
	}

	public void setInComingEdges(Vector<Edge> inComingEdges) {
		this.inComingEdges = inComingEdges;
	}

	public Vector<Edge> getOutgoingEdges() {
		return outgoingEdges;
	}

	public void setOutgoingEdges(Vector<Edge> outgoingEdges) {
		this.outgoingEdges = outgoingEdges;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/*
	 * This function returns true if the node passed in the first parameter
	 * exists in the list of nodes passed in the second parameter
	 */
	public boolean hasNode(Node n, Vector<Node> nodeList) {
		int numSuccs = nodeList.size();
		for (int i = 0; i < numSuccs; i++) {
			Node nodeFound = nodeList.elementAt(i);
			if (nodeFound == n)
				return true;
		}
		return false;
	}

	/*
	 * This function returns true if the node has an edge to the passed node as
	 * parameter
	 */
	public boolean goesTo(Node n) {
		return hasNode(n, successors);
	}

	public Object clone() throws java.lang.CloneNotSupportedException {
		Node copy;
		copy = (Node) super.clone();
		return copy;
	}


	public boolean hasChild(Node child)
	{
		return successors.indexOf(child) != -1 || predecessors.indexOf(child) != -1;
	}
	
	
}
